package com.hoshiicloud.user.service.impl.organization;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.user.entity.organization.StoreOrganization;
import com.hoshiicloud.user.entity.organization.StoreOrganizationDepartmentes;
import com.hoshiicloud.user.feign.IdGeneratorService;
import com.hoshiicloud.user.mapper.organization.StoreOrganizationDepartmentesMapper;
import com.hoshiicloud.user.service.organization.StoreOrganizationDepartmentesService;
import com.hoshiicloud.user.service.organization.StoreOrganizationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 店铺机构（供应商）部门 服务实现类
 * </p>
 *
 * @author campchen
 * @since 2019-05-15
 */
@Slf4j
@Service
public class StoreOrganizationDepartmentesServiceImpl extends ServiceImpl<StoreOrganizationDepartmentesMapper, StoreOrganizationDepartmentes> implements StoreOrganizationDepartmentesService {

    @Autowired
    private IdGeneratorService idGeneratorService;
    @Autowired
    private StoreOrganizationService storeOrganizationService;

    /**
     * @return IPage
     * @description: 分页查询店铺机构（供应商）部门
     */
    @Override
    @Logable(authName = "供应商/机构/合作伙伴-店铺机构（供应商）部门管理-分页查询店铺机构（供应商）部门")
    @Cacheable(cacheNames = "StoreOrganizationDepartmentes",key ="#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    public IPage<StoreOrganizationDepartmentes> getPage(IPage page, QueryWrapper queryWrapper,String key) {
        return baseMapper.selectPage(page, queryWrapper);
    }

    /**
     * @param id
     * @return com.hoshiicloud.organization.entity.StoreOrganizationDepartmentes
     * @description: 根据id查询店铺机构（供应商）部门
     */
    @Override
    @Cacheable(cacheNames = "StoreOrganizationDepartmentes", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "供应商/机构/合作伙伴-店铺机构（供应商）部门 -查询店铺机构（供应商）部门 ")
    public StoreOrganizationDepartmentes getById(Long id) {
        return baseMapper.selectById(id);
    }


    /**
     * @return boolean
     * @description: 新增店铺机构（供应商）部门
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "StoreOrganizationDepartmentes", allEntries = true)
    @Logable(authName = "供应商/机构/合作伙伴-店铺机构（供应商）部门-新增店铺机构（供应商）部门")
    public String insert(StoreOrganizationDepartmentes entity) {
        String id = idGeneratorService.queryGeneratorId();
        if (StringUtils.isBlank(id)) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        StoreOrganization storeOrganization = storeOrganizationService.getById(entity.getOrganizationId());
        if ( null == storeOrganization) {
            throw new ServiceException(CommonError.error("新增失败，机构/供应商Id不存在"));
        }
        entity.setId(Long.parseLong(id));
        entity.setCreatedTime(LocalDateTime.now());
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return id;
    }

    /**
     * @return boolean
     * @description: 修改店铺机构（供应商）部门
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "StoreOrganizationDepartmentes", allEntries = true)
    @Logable(authName = "供应商/机构/合作伙伴-店铺机构（供应商）部门管理-修改店铺机构（供应商）部门")
    public boolean updateById(StoreOrganizationDepartmentes entity) {
        entity.setModifiedTime(LocalDateTime.now());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        return true;
    }



    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "StoreOrganizationDepartmentes", allEntries = true)
    @Logable(authName = "供应商/机构/合作伙伴-店铺机构（供应商）部门管理-删除店铺机构（供应商）部门")
    public boolean deleteStoreOrganizationDepartmentesById(Long id) {
        StoreOrganizationDepartmentes entity = this.getById(id);
        if( null == entity){
            throw new ServiceException("删除店铺机构部门异常，无法找到店铺机构部门记录");
        }
        entity.setModifiedTime(LocalDateTime.now());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("删除失败"));
        }
        return true;
    }
}
